package com.github.hongkaiwen.rxjava.play.pratice;

import com.github.hongkaiwen.rxjava.play.RXHttpUtil;
import io.reactivex.Observable;

/**
 * 密级别：classify:p2#- User: hongkai Date: 15/08/2018 11:44 AM Description:
 */
public class RxHttpPlay {
    private static long start = System.currentTimeMillis();

    public static void main(String args[]) {
        Observable.zip(count("http://localhost:8999/sleep?s=5")
            , content("http://localhost:8999/sleep?s=10"), (i, j) -> build(i, j))
//            .doOnNext()  做缓存
            .subscribe(RxHttpPlay::result);
    }

    private static void result(String result){
        System.out.println(System.currentTimeMillis() - start);
        System.out.println(result);
        System.exit(0);
    }

    private static void cache(String input){
        //TODO
    }

    private static String build(int count, String content){
        return String.format("[%s] first site response length: %d, second site response: %s", Thread.currentThread(),count, content);
    }


    private static Observable<Integer> count(String url) {
        return RXHttpUtil.getBody(url).map(t -> t.length());
    }

    private static Observable<String> content(String url) {
        return RXHttpUtil.getBody(url);
    }
}
